import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:heytea_kit/hexie/hexie.dart';
import 'package:heytea_kit/heytea_kit.dart';

void main() {
  const MethodChannel channel = MethodChannel('heytea_kit');

  TestWidgetsFlutterBinding.ensureInitialized();

  setUp(() {
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      return '42';
    });
  });

  tearDown(() {
    channel.setMockMethodCallHandler(null);
  });

  test('getPlatformVersion', () async {
    expect(await HeyTeaKit.platformVersion, '42');
  });

  Future<Hexie> buildHexie() async {
    final hexie = Hexie();
    final file = File('assets/sensitive_words.txt');
    await hexie.build([file]);

    return hexie;
  }

  // 317ms
  test('敏感词字典', () async {
    await buildHexie();
  });

  // 600ms
  test('敏感词匹配', () async {
    final hexie = await buildHexie();

    final buffer = StringBuffer();
    for (int i = 0; i < 1000; i++) {
      buffer.write('一句无意义的废话，用来拖时间的');
    }
    buffer.write('我操你妈习近平');

    final text = buffer.toString();
    final found = await hexie.firstSensitive(text);
    print('总字数：${text.length}, 敏感词：$found');
  });
}
